package com.catmiw.stock2013.util.dbf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

// Referenced classes of package cn.edu.sut.oa.workadmin.sjcl:
//               JDBFException

public class DBFField {
	private String name;
	private char type;
	private int length;
	private int decimalCount;

	public DBFField(String name, char type, int length, int decimalCount) throws DBFException {
		if (name.length() > 10)
			throw new DBFException(
					"The field name is more than 10 characters long: " + name);
		if (type != 'C' && type != 'N' && type != 'L' && type != 'D' && type != 'F')
			throw new DBFException("The field type is not a valid. Got: " + type);
		if (length < 1)
			throw new DBFException(
					"The field length should be a positive integer. Got: " + length);
		if (type == 'C' && length >= 254)
			throw new DBFException(
					"The field length should be less than 254 characters for character fields. Got: "
							+ length);
		if (type == 'N' && length >= 21)
			throw new DBFException(
					"The field length should be less than 21 digits for numeric fields. Got: "
							+ length);
		if (type == 'L' && length != 1)
			throw new DBFException(
					"The field length should be 1 characater for logical fields. Got: "
							+ length);
		if (type == 'D' && length != 8)
			throw new DBFException(
					"The field length should be 8 characaters for date fields. Got: "
							+ length);
		if (type == 'F' && length >= 21)
			throw new DBFException(
					"The field length should be less than 21 digits for floating point fields. Got: "
							+ length);
		if (decimalCount < 0)
			throw new DBFException(
					"The field decimal count should not be a negative integer. Got: "
							+ decimalCount);
		if ((type == 'C' || type == 'L' || type == 'D') && decimalCount != 0)
			throw new DBFException(
					"The field decimal count should be 0 for character, logical, and date fields. Got: "
							+ decimalCount);
		if (decimalCount > length - 1) {
			throw new DBFException(
					"The field decimal count should be less than the length - 1. Got: "
							+ decimalCount);
		} else {
			this.name = name;
			this.type = type;
			this.length = length;
			this.decimalCount = decimalCount;
			return;
		}
	}

	public String getName() {
		return name;
	}

	public char getType() {
		return type;
	}

	public int getLength() {
		return length;
	}

	public int getDecimalCount() {
		return decimalCount;
	}

	public String format(Object obj) throws DBFException {
		if (type == 'C' || type == 'N') {
			if (obj == null)
				obj = "";
			if (obj instanceof String) {
				String s = (String) obj;
				if (s.length() > getLength())
					throw new DBFException("'" + obj + "' is longer than "
							+ getLength() + " characters.");
				StringBuffer stringbuffer = new StringBuffer(getLength()
						- s.length());
				for (int i = 0; i < getLength() - s.length(); i++)
					stringbuffer.append(' ');

				return s + stringbuffer;
			} else {
				throw new DBFException("Expected a String, got "
						+ obj.getClass() + ".");
			}
		}
		if (type == 'L') {
			if (obj == null)
				obj = new Boolean(false);
			if (obj instanceof Boolean) {
				Boolean boolean1 = (Boolean) obj;
				return boolean1.booleanValue() ? "Y" : "N";
			} else {
				throw new DBFException("Expected a Boolean, got "
						+ obj.getClass() + ".");
			}
		}
		if (type == 'D') {
			if (obj == null)
				obj = new Date();
			if (obj instanceof Date) {
				Date date = (Date) obj;
				SimpleDateFormat simpledateformat = new SimpleDateFormat(
						"yyyyMMdd");
				return simpledateformat.format(date);
			} else {
				throw new DBFException("Expected a Date, got "
						+ obj.getClass() + ".");
			}
		} else {
			throw new DBFException("Unrecognized JDBFField type: " + type);
		}
	}

	public Object parse(String s) throws DBFException {
		s = s.trim();
		if (type == 'N' || type == 'F') {
			if (s.equals(""))
				s = "0";
			try {
				if (getDecimalCount() == 0)
					return new Long(s);
				else
					return new Double(s);
			} catch (NumberFormatException numberformatexception) {
				throw new DBFException(numberformatexception);
			}
		}
		if (type == 'C')
			return s;
		if (type == 'L') {
			if (s.equals("Y") || s.equals("y") || s.equals("T")
					|| s.equals("t"))
				return new Boolean(true);
			if (s.equals("N") || s.equals("n") || s.equals("F")
					|| s.equals("f"))
				return new Boolean(false);
			else
				throw new DBFException(
						"Unrecognized value for logical field: " + s);
		}
		if (type == 'D') {
			SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
			try {
				if ("".equals(s))
					return null;
				else
					return simpledateformat.parse(s);
			} catch (ParseException parseexception) {
				throw new DBFException(parseexception);
			}
		} else {
			throw new DBFException("Unrecognized JDBFField type: " + type);
		}
	}

	public String toString() {
		return "[field:"+name+"]";
	}

}
